<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>for循环</title>
    <style>
      span {
        display: inline-block;
        width: 80px;
      }
    </style>
  </head>
  <body>
    <script>
      /* 计算1-100之间奇数的和 */
      /* for (var i = 1, sum = 0; i <= 100; ++i) {
        if (i & 1) {
          sum += i;
        }
      }
      console.log(sum); */

      /* 水仙花：每个位上的数字的3次幂之和等于其自身 */
      /* for (var i = 100, result = []; i < 1000; ++i) {
        var aaa = (i + "")[2],
          aa = (i + "")[1],
          a = (i + "")[0];

        var sum = aaa * aaa * aaa + aa * aa * aa + a * a * a;
        if (sum === i) {
          result.push(i);
        }
      }
      console.log(result); */

      /* 从用户输入一个数，判断其是否是质数(只能被1和其本身整除) */
      /* var num = +prompt("请输入一个数");
      if (num <= 1 || isNaN(num)) {
        alert("请重新输入");
      } else {
        for (var i = 2, flag = true; i < num; ++i) {
          console.log(i);
          if (num % i === 0) {
            flag = false;
            break;
          }
        }
        if (flag) {
          alert(num + "是质数");
        } else {
          alert(num + "不是质数");
        }
      } */

      /* 打印输出星星 */
      /* for (var i = 0; i < 5; ++i) {
        for (var j = 0; j < 5 - i; ++j) {
          document.write("*&nbsp;&nbsp;");
        }
        document.write("<br />");
      } */

      /* 打印乘法表 */
      /* for (var i = 1; i <= 9; ++i) {
        for (var j = 1; j <= i; ++j) {
          document.write(`<span>${j}*${i}=${i * j}</span>`);
        }
        document.write("<br />");
      } */

      /* 打印2-100之间的质数 */
      for (var i = 2; i <= 100; ++i) {
        var flag = true;
        for (var j = 2; j < i; ++j) {
          if (j % i === 0) {
            flag = false;
          }
        }
        if (flag) {
          console.log("质数" + i);
        }
      }
    </script>
  </body>
</html>
